package com.lagou.minicat.core;

import org.dom4j.Document;
import org.dom4j.Element;

public class Service {

    private Connector connector;
    private Engine engine;

    public void init(Document serverConfigDoc) {
        Element rootElement = serverConfigDoc.getRootElement();

        // 初始化Engine
        Engine engine = new Engine();
        engine.init(serverConfigDoc);
        this.engine = engine;

        // 初始化Connector
        Element connectorEle = (Element) rootElement.selectSingleNode("/Server/Service/Connector");
        String port = connectorEle.attributeValue("port");
        Mapper mapper = new Mapper();
        engine.getHosts().forEach(mapper::registerHost);
        this.connector = new Connector(Integer.valueOf(port), mapper);
    }

    public Connector getConnector() {
        return connector;
    }

    public void setConnector(Connector connector) {
        this.connector = connector;
    }

    public Engine getEngine() {
        return engine;
    }

    public void setEngine(Engine engine) {
        this.engine = engine;
    }
}
